package com.ruoyi.web.controller.ksdH5;


import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.enums.BusinessStatus;
import com.ruoyi.common.utils.Constans;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;

/**
 * 空卡代APP 卡信息
 * 
 * @author ruoyi
 * @date 2019-06-27
 */
@Controller		 
@RequestMapping("/h5/quick")
public class KsdH5QuickController 
{
	
	/**
	  * 收款明细
	  * @param request
	  * @param response
	  * @return
	 * @throws UnsupportedEncodingException 
	  */
	 @GetMapping("list")
	 public String list(HttpServletRequest request,HttpServletResponse response,ModelMap map) throws UnsupportedEncodingException {
		 boolean checkSession = Ksdh5Util.checkSession(request, response);
		//判断没有登陆状态
			if (!checkSession) {
				return Constans.KSD_APP_H5_LOGIN_URL;
			}
			 boolean checkRealName = Ksdh5Util.checkRealName(request, response);
			 if (!checkRealName) {
				//跳转实名认证页面
					return "h5/realName/realName";
				}
			
			HttpSession session = request.getSession();
			Object token = session.getAttribute("token");
			String  head ="v="+Constans.KSD_APP_H5_VERSION+","
					+ "token="+token.toString();
			String params="";
			params.replaceAll(" ", "");
			String  result = HttpUtils.sendGet(Constans.KSD_APP_H5_URL+"/k/getKuaijie", params,head);
		    Map<String, Object> reMap = Ksdh5Util.getMap(result);
		    map.put("obj", reMap.get("obj"));
			return "h5/quick/list";
	 } 
	 /**
	  * 跳转计划列表页面
	  * @param request
	  * @param response
	  * @return
	  */
	 @GetMapping("quick")
	 public String toQuickPage(HttpServletRequest request,HttpServletResponse response) {
		 boolean checkSession = Ksdh5Util.checkSession(request, response);
			//判断没有登陆状态
			if (!checkSession) {
				return Constans.KSD_APP_H5_LOGIN_URL;
			}
			 boolean checkRealName = Ksdh5Util.checkRealName(request, response);
			 if (!checkRealName) {
				//跳转实名认证页面
					return "h5/realName/realName";
				}
			
		return "h5/quick/quick";
	 } 
	 

	 /**
	  * 跳转快捷选通道页面
	  * @param request
	  * @param response
	  * @return
	  */
	 @GetMapping("toAcq")
	 public String toItemPage(HttpServletRequest request,HttpServletResponse response,ModelMap map) {
		 boolean checkSession = Ksdh5Util.checkSession(request, response);
			//判断没有登陆状态
			if (!checkSession) {
				return Constans.KSD_APP_H5_LOGIN_URL;
			}
			 boolean checkRealName = Ksdh5Util.checkRealName(request, response);
			 if (!checkRealName) {
				//跳转实名认证页面
					return "h5/realName/realName";
				}
			HttpSession session = request.getSession();
			 String cardNo = request.getParameter("cardNo");
			 String bankId = request.getParameter("bankId");
			 String money = request.getParameter("money");
			 Object token = session.getAttribute("token");
			 String  head ="v="+Constans.KSD_APP_H5_VERSION+","
					+ "token="+token.toString();
			String params="money="+money+"&"
						+"bankId="+bankId;
			String  result = HttpUtils.sendGet(Constans.KSD_APP_H5_URL+"/k/wkAcqInfo", params,head);
		   map.addAttribute("result", Ksdh5Util.getMap(result));//
		   map.addAttribute("quickMoney", money);//快捷金额
		   map.addAttribute("bankId", bankId);//卡id
		   map.addAttribute("cardNo", cardNo);//卡编号
		   return "h5/quick/acqList";
	 } 
	
	 /**
	  * 验证通道是否开通
	  * @param request
	  * @param response
	  * @return
	  */
	 @PostMapping("checkAcq")
	 @ResponseBody
	 public Map<String, Object> checkAcq(HttpServletRequest request,HttpServletResponse response) {
		 Map<String, Object> map = new HashMap<>();
		 boolean checkSession = Ksdh5Util.checkSession(request, response);
			//判断没有登陆状态
			if (!checkSession) {
				map.put("status", "out");
				return map;
			}
			 boolean checkRealName = Ksdh5Util.checkRealName(request, response);
			 if (!checkRealName) {
				//跳转实名认证页面
				 map.put("status", "NoReal");
				 return map;
				}
			
			HttpSession session = request.getSession();
			Object token = session.getAttribute("token");
			 String bankcard = request.getParameter("bankcard");//卡号
			 String AcqCode = request.getParameter("AcqCode");//通道
			 String accmoney = request.getParameter("accmoney");
			String  head ="v="+Constans.KSD_APP_H5_VERSION+","
					+ "token="+token.toString();
			String params="bankcard="+bankcard+"&"
					     +"AcqCode="+AcqCode+"&"
					     +"accmoney="+accmoney;
			String  result = HttpUtils.sendPost(Constans.KSD_APP_H5_URL+"/k/checkStatus", params,head);
			return Ksdh5Util.getMap(result);
	 } 
	 
	 /**
	  *预绑卡  发送绑定通道验证码 
	  * @param request
	  * @param response
	  * @return
	  */
	 @PostMapping("prebind")
	 @ResponseBody
	 public Map<String, Object> prebind(HttpServletRequest request,HttpServletResponse response) {
		 Map<String, Object> map = new HashMap<>();
		 boolean checkSession = Ksdh5Util.checkSession(request, response);
			//判断没有登陆状态
			if (!checkSession) {
				map.put("status", "out");
				return map;
			}
			 boolean checkRealName = Ksdh5Util.checkRealName(request, response);
			 if (!checkRealName) {
				//跳转实名认证页面
				 map.put("status", "NoReal");
				 return map;
				}
			
			HttpSession session = request.getSession();
			Object token = session.getAttribute("token");
			 String cardid = request.getParameter("cardid");//卡号
			 String acqcode = request.getParameter("acqcode");//通道
			String  head ="v="+Constans.KSD_APP_H5_VERSION+","
					+ "token="+token.toString();
			String params="cardid="+cardid+"&"
					     +"acqcode="+acqcode;
			String  result = HttpUtils.sendPost(Constans.KSD_APP_H5_URL+"/p/prebind", params,head);
			return Ksdh5Util.getMap(result);
	 } 
	 
	
	 /**
	  * 验证快捷交易密码并发送验证码
	  * @param request
	  * @param response
	  * @return
	 * @throws UnsupportedEncodingException 
	  */
	 @PostMapping("checkTransPW")
	 @ResponseBody
	 public Map<String, Object> checkTransPW(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
		 Map<String, Object> map = new HashMap<>();
		 boolean checkSession = Ksdh5Util.checkSession(request, response);
			//判断没有登陆状态
			if (!checkSession) {
				map.put("status", "out");
				return map;
			}
			 boolean checkRealName = Ksdh5Util.checkRealName(request, response);
			 if (!checkRealName) {
				//跳转实名认证页面
				 map.put("status", "NoReal");
				 return map;
				}
			
			HttpSession session = request.getSession();
			Object token = session.getAttribute("token");
			 String transPW = request.getParameter("transPW");
			 String acqcode = request.getParameter("acqcode");
			 String cardid = request.getParameter("cardid");
			 String money = request.getParameter("money");
			 String addr = request.getParameter("addr");
			 if(StringUtils.isNotEmpty(addr)) {
				 addr = addr.substring(addr.indexOf("-"));
			 }
			String  head ="v="+Constans.KSD_APP_H5_VERSION+","
					+ "token="+token.toString();
			String params="transPW="+Ksdh5Util.md5Str(transPW)+"&"
						 +"acqcode="+acqcode+"&"
						 +"cardid="+cardid+"&"
						 +"money="+money+"&"
						 +"addr="+URLEncoder.encode(addr, "utf-8");
			params.replaceAll(" ", "");
			String  result = HttpUtils.sendPost(Constans.KSD_APP_H5_URL+"/k/checkTransPW", params,head);
			Map<String, Object> map2 = Ksdh5Util.getMap(result);
			if(BusinessStatus.SUCCESS.equals(map2.get("status"))) {
				//交易密码验证成功，请求发验证码接口
				String  params2 ="cardid="+cardid+"&"
						 +"acqcode="+acqcode+"&"
						 +"money="+money+"&"
						 +"addr="+URLEncoder.encode(addr, "utf-8");
				String  result2 = HttpUtils.sendPost(Constans.KSD_APP_H5_URL+"/k/prefast", params2,head);
				return Ksdh5Util.getMap(result2);
			}
			return map2;
	 } 
	 
	 
	 /**
	  * 提交快捷交易
	  * @param request
	  * @param response
	  * @return
	 * @throws UnsupportedEncodingException 
	  */
	 @PostMapping("comfirmfast")
	 @ResponseBody
	 public Map<String, Object> comfirmfast(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
		 Map<String, Object> map = new HashMap<>();
		 boolean checkSession = Ksdh5Util.checkSession(request, response);
			//判断没有登陆状态
			if (!checkSession) {
				map.put("status", "out");
				return map;
			}
			 boolean checkRealName = Ksdh5Util.checkRealName(request, response);
			 if (!checkRealName) {
				//跳转实名认证页面
				 map.put("status", "NoReal");
				 return map;
				}
			
			HttpSession session = request.getSession();
			Object token = session.getAttribute("token");
			 String smscode = request.getParameter("smscode");
			 String resultParam = request.getParameter("result");
			 String addr = request.getParameter("addr");
			 addr = addr.substring(addr.indexOf("-"));
			 String money = request.getParameter("money");
			 String acqcode = request.getParameter("acqcode");
			 String cardid = request.getParameter("cardid");
			
			String  head ="v="+Constans.KSD_APP_H5_VERSION+","
					+ "token="+token.toString();
			String params="smscode="+smscode+"&"
						 +"result="+URLEncoder.encode(resultParam, "utf-8")+"&"
						 +"addr="+URLEncoder.encode(addr, "utf-8")+"&"
						 +"money="+money+"&"
						 +"acqcode="+acqcode+"&"
						 +"cardid="+cardid;
			params.replaceAll(" ", "");
			String  result = HttpUtils.sendPost(Constans.KSD_APP_H5_URL+"/k/comfirmfast", params,head);
			return Ksdh5Util.getMap(result);
	 } 
	 
	 
}